<?php
namespace app\user\controller;

use app\common\controller\UserBase;
use app\common\model\Field as FieldModel;
use app\user\model\FieldData;
use app\user\model\UserGroup;
use app\user\model\UserRule;

class Field extends UserBase
{
    protected $noAuth = ['get_field'];
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->fetch('index');
    }
    public function index_json($limit='15',$field_id)
    {
        $field = new FieldData();
        if($field_id){
            $field = $field->where('field_id',$field_id);
        }
        $list = $field->order('sort_order asc,id desc')->paginate($limit);
        $this->result($list);
    }
    //获取自定义字段类型
    public function get_field()
    {
        $field = new FieldModel();
        //判断权限-获取我的权限
        $auth_id = UserRule::my_auth();
        $field = $field->where('auth_id','in',$auth_id);
        $list = $field->order('auth_id asc,id desc')->select();
        $data = [];
        foreach ($list as $k=>$r){
            $data[$k]['leftid'] = $k;
            $data[$k]['fieldId'] = $r['id'];
            $data[$k]['fieldName'] = $r['name'];
        }
        return json(['code'=>'0','msg'=>'','count'=>count($list),'data'=>$data]);
    }
    //初始化字段
    public function default($field_id)
    {
        $result = FieldData::CreateDefaultField($field_id);
        $this->success($result);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'fieldData');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //检查该字段是否必须
            FieldData::IsSysField($param['field_id'],$param['name']) && $this->error('系统保留字段!请修改字段标识');
            $result = FieldData::create($param);
            if ($result == true) {
                insert_user_log('添加了自定义字段');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save');
    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param  = $this->request->param();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'fieldDataEdit');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //更新数据
            $resule = FieldData::update($param,['id'=>$param['id']]);
            if ( $resule == true) {
                insert_user_log('修改了自定义名称');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
            'data'  => FieldData::where('id',$id)->find(),
        ]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            FieldData::destroy($param['id']);
            insert_user_log('删除了自定义名称');
            $this->success('删除成功');
            
        }
    }

}
